// pages/Index/myCouponBag.js
const controller = require("../controllers/Controller").controller

Page({

  /**
   * 页面的初始数据
   */
  data: {
    tabList: [{
      text: "商圈券",
      type: 1
    }, {
      text: "单店优惠券",
      type: 0
    }, {
      text: "好友券",
      type: 3
    }],
    selectIndex: 0,
    couponType: 1,
    myList: [],
    otherList: [],
    isOther: false,
    shopDetail:""
  },

  //跳转领券中心
  toReceiveCenter(){
    wx.navigateTo({
      url: '/pages/Mall/Coupon',
    })
  },

  //跳转券详情
  toDetail(e){
    let id=e.currentTarget.dataset.id
    let couponAccountId=e.currentTarget.dataset.haveid
    let couponType=e.currentTarget.dataset.paytype
    let couponBagId=e.currentTarget.dataset.couponbagid
    let shopId=e.currentTarget.dataset.shopid
    if(couponType==0){
      wx.navigateTo({
        url: '/pages/Mall/Coupondetail?id='+id+"&couponAccountId="+couponAccountId+"&couponType="+couponType,
      })
    }else{
      wx.navigateTo({
        url: '/pages/Mall/couponDetailBuy?id='+id+"&couponAccountId="+couponAccountId+'&couponBagId='+couponBagId+"&out=1"+"&shopId="+shopId,
      })
    }
  },

  //切换tab
  changeTab(e) {
    let index = e.currentTarget.dataset.index
    this.setData({
      selectIndex: index,
      couponType: this.data.tabList[index].type
    })
    if(this.data.couponType!=3){
      this.setData({
        isOther:false
      })
      this.getCouponList()
    }else{
      this.setData({
        couponType:'',
        isOther:true
      })
      this.getCouponList()
    }
  },

  //获取优惠券列表
  getCouponList() {
    let value = {}
    console.log(wx.getStorageSync('shopId'))
    value.shopId = wx.getStorageSync('shopId')
    value.couponType = this.data.couponType

    controller.getCouponList(value).then(res => {
      res.data.my.forEach(item=>{
        if(item.effectiveStartTime){
          item.effectiveStartTime=item.effectiveStartTime.split(" ")[0]
          item.effectiveEndTime=item.effectiveEndTime.split(" ")[0]
        }
      })

      res.data.other.forEach(item=>{
        if(item.effectiveStartTime){
          item.effectiveStartTime=item.effectiveStartTime.split(" ")[0]
          item.effectiveEndTime=item.effectiveEndTime.split(" ")[0]
        }
      })
      this.setData({
        myList: res.data.my,
        otherList: res.data.other
      })
    })
  },

  //获取店铺详情
  getShopDetail(){
    controller.shopDetails({shopId:wx.getStorageSync('shopId')}).then(res=>{
      this.setData({
        shopDetail:res.data
      })
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getCouponList()
    this.getShopDetail()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  // onShareAppMessage: function () {

  // }
})